﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UofCASE.GreenPepe.GPTest;

namespace UofCASE.AgileProductLinerDSL.Nant
{
    class FitnesseXMLReport
    {

        Dictionary<String, String> results = new Dictionary<string,string>();

        internal void AddResultFile(UofCASE.GreenPepe.GPTest.Test test)
        {
            String ret = @"
            <result>
	            <relativePageName>" + test.TestFile.InputFile.FullName + @"</relativePageName>
                <tags></tags>
	            <counts>
		            <right>" + test.TestResult.RightCount + @"</right>
		            <wrong>" + test.TestResult.WrongCount + @"</wrong>
		            <ignores>" + test.TestResult.IgnoredCount + @"</ignores>
		            <exceptions>" + test.TestResult.ExceptionCount + @"</exceptions>
	            </counts>" +
//	            @"<content>" + fixXML(System.IO.File.ReadAllText(test.TestFile.OutputFile.FullName)) + @"</content>" +
                @"<tables>
                </tables>
            </result>";
            results.Add(test.TestFile.InputFile.Name, ret);
        }

        internal String fixXML(String str)
        {
            str = str.Replace("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">", "");
            str = str.Replace("<font size=-2>", "<font size=\"-2\">");
            str = str.Replace("style='background-color: #FFFFAA;'", "style=\"background-color: #FFFFAA;\"");
            return str;
        }

        internal void SaveTo(string ResultsDir)
        {
            StringBuilder buff = new StringBuilder(header());
            foreach (String result in results.Keys.OrderBy(a => a))
            {
                buff.Append(results[result]);
            }
            buff.Append(footer());

            System.IO.File.WriteAllText(ResultsDir + "/results.xml",  buff.ToString());
        }

        private String header(){
            return "<testResults><rootPath>SuiteAcceptanceTests</rootPath>";
        }

        private String footer(){
            return "</testResults>";
        }
    }
}
